package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.OrgEquipmenBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by z on 2018/9/16.
 * Des:机电设备台账
 */

public class EquipmentArchivesActivity extends CommonListActivity {
    private android.support.v7.widget.SearchView searchView;
    private MySpinner mySpinner;
    private OrgListBean orgListBean;
    private String orgId;
    private OrgEquipmenBean orgEquipmenBean;
    private Button btn_search;

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EquipmentArchivesAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return "机电设备台账";
    }

    @Override
    public void initData() {
        super.initData();
        View view = LayoutInflater.from(this).inflate(R.layout.activity_equipment_archives_hander, null);
        searchView = view.findViewById(R.id.searchView);
        mySpinner = view.findViewById(R.id.mySpinner);
        btn_search = (Button) view.findViewById(R.id.btn_search);
        flContent.addView(view);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getListData(orgId, searchView.getQuery().toString());
            }
        });
        searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(searchView.getQuery())) {
                    getListData(orgId, searchView.getQuery().toString());
                    return true;
                }
                return false;
            }
        });
//        searchView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if (!TextUtils.isEmpty(sousuoEdit.getText())) {
//                    getListData(orgId, sousuoEdit.getText().toString());
//                    return true;
//                }
//                return false;
//            }
//        });
//        mySpinner.getmText().setVisibility(View.GONE);
//        mySpinner.getmHitImage().setVisibility(View.GONE);
//        mySpinner.getmCenterText().setVisibility(View.VISIBLE);
//        mySpinner.getmCenterText().setTextSize(14);
//        mySpinner.getmCenterText().setTextColor(getResources().getColor(R.color.white));
//        mySpinner.getmRelativeLayout().setBackground(getResources().getDrawable(R.drawable.shape_rect_red));
        getHttpData();
    }

    /**
     * 获取机构
     */
    private void getHttpData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }
                orgId = orgListBean.getList().get(0).getId();
                getListData(orgId, "");
                mySpinner.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        orgId = orgListBean.getList().get(postion).getId();
                        getListData(orgId, "");
                    }
                });
            }
        });
    }

    //获取列表数据
    private void getListData(String orgId, String name) {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("orgId", orgId);
        hashMap.put("name", name);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgEquipmentList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                orgEquipmenBean = GsonUtil.GsonToBean(response, OrgEquipmenBean.class);
                mAdapter.updateItems(orgEquipmenBean.getList());
            }
        });
    }


    public class EquipmentArchivesAdapter extends BaseQuickAdapter<OrgEquipmenBean.ListBean> {

        public EquipmentArchivesAdapter(Context context) {
            super(context);
        }

        @Override
        protected int attachLayoutRes() {
            return R.layout.item_equipment_archives_layout;
        }

        @Override
        protected void convert(BaseViewHolder holder, OrgEquipmenBean.ListBean item) {
            holder.setText(R.id.numText, holder.getPosition() + 1 + "");
            holder.setText(R.id.titleText, item.getProName());
            holder.setText(R.id.taishuText, item.getNum() + "台");
            holder.setText(R.id.pinpaiText, item.getModel());
            holder.setText(R.id.bianhaoText, item.getManufacturer());
        }
    }
}
